package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMechanicsAllPowerfulMachineDetailBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMechanicsAllPowerfulMachineDetailVo;
import com.ruoyi.zhgd.domain.NLaboratoryMechanicsAllPowerfulMachineDetail;
import com.ruoyi.zhgd.mapper.NLaboratoryMechanicsAllPowerfulMachineDetailMapper;
import com.ruoyi.zhgd.service.INLaboratoryMechanicsAllPowerfulMachineDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 万能机TestMaster软件上传详情数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-11
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryMechanicsAllPowerfulMachineDetailServiceImpl implements INLaboratoryMechanicsAllPowerfulMachineDetailService {

    private final NLaboratoryMechanicsAllPowerfulMachineDetailMapper baseMapper;

    /**
     * 查询万能机TestMaster软件上传详情数据
     */
    @Override
    public NLaboratoryMechanicsAllPowerfulMachineDetailVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询万能机TestMaster软件上传详情数据列表
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsAllPowerfulMachineDetailVo> queryPageList(NLaboratoryMechanicsAllPowerfulMachineDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryMechanicsAllPowerfulMachineDetail> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryMechanicsAllPowerfulMachineDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询万能机TestMaster软件上传详情数据列表
     */
    @Override
    public List<NLaboratoryMechanicsAllPowerfulMachineDetailVo> queryList(NLaboratoryMechanicsAllPowerfulMachineDetailBo bo) {
        LambdaQueryWrapper<NLaboratoryMechanicsAllPowerfulMachineDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryMechanicsAllPowerfulMachineDetail> buildQueryWrapper(NLaboratoryMechanicsAllPowerfulMachineDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryMechanicsAllPowerfulMachineDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSampleId()), NLaboratoryMechanicsAllPowerfulMachineDetail::getSampleId, bo.getSampleId());
        lqw.eq(StringUtils.isNotBlank(bo.getSampleKey()), NLaboratoryMechanicsAllPowerfulMachineDetail::getSampleKey, bo.getSampleKey());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailKey()), NLaboratoryMechanicsAllPowerfulMachineDetail::getDetailKey, bo.getDetailKey());
        lqw.eq(StringUtils.isNotBlank(bo.getTime()), NLaboratoryMechanicsAllPowerfulMachineDetail::getTime, bo.getTime());
        lqw.eq(StringUtils.isNotBlank(bo.getLo()), NLaboratoryMechanicsAllPowerfulMachineDetail::getLo, bo.getLo());
        lqw.eq(StringUtils.isNotBlank(bo.getFm()), NLaboratoryMechanicsAllPowerfulMachineDetail::getFm, bo.getFm());
        lqw.eq(StringUtils.isNotBlank(bo.getRm()), NLaboratoryMechanicsAllPowerfulMachineDetail::getRm, bo.getRm());
        lqw.eq(StringUtils.isNotBlank(bo.getFeH()), NLaboratoryMechanicsAllPowerfulMachineDetail::getFeH, bo.getFeH());
        lqw.eq(StringUtils.isNotBlank(bo.getReH()), NLaboratoryMechanicsAllPowerfulMachineDetail::getReH, bo.getReH());
        lqw.eq(StringUtils.isNotBlank(bo.getFeL()), NLaboratoryMechanicsAllPowerfulMachineDetail::getFeL, bo.getFeL());
        lqw.eq(StringUtils.isNotBlank(bo.getReL()), NLaboratoryMechanicsAllPowerfulMachineDetail::getReL, bo.getReL());
        lqw.eq(StringUtils.isNotBlank(bo.getLu()), NLaboratoryMechanicsAllPowerfulMachineDetail::getLu, bo.getLu());
        lqw.eq(StringUtils.isNotBlank(bo.getA()), NLaboratoryMechanicsAllPowerfulMachineDetail::getA, bo.getA());
        lqw.eq(StringUtils.isNotBlank(bo.getLc()), NLaboratoryMechanicsAllPowerfulMachineDetail::getLc, bo.getLc());
        lqw.eq(StringUtils.isNotBlank(bo.getStdRe()), NLaboratoryMechanicsAllPowerfulMachineDetail::getStdRe, bo.getStdRe());
        lqw.eq(StringUtils.isNotBlank(bo.getSu()), NLaboratoryMechanicsAllPowerfulMachineDetail::getSu, bo.getSu());
        lqw.eq(StringUtils.isNotBlank(bo.getZ()), NLaboratoryMechanicsAllPowerfulMachineDetail::getZ, bo.getZ());
        lqw.eq(StringUtils.isNotBlank(bo.getFp()), NLaboratoryMechanicsAllPowerfulMachineDetail::getFp, bo.getFp());
        lqw.eq(StringUtils.isNotBlank(bo.getRp()), NLaboratoryMechanicsAllPowerfulMachineDetail::getRp, bo.getRp());
        lqw.eq(StringUtils.isNotBlank(bo.getFt()), NLaboratoryMechanicsAllPowerfulMachineDetail::getFt, bo.getFt());
        lqw.eq(StringUtils.isNotBlank(bo.getRt()), NLaboratoryMechanicsAllPowerfulMachineDetail::getRt, bo.getRt());
        lqw.eq(StringUtils.isNotBlank(bo.getE()), NLaboratoryMechanicsAllPowerfulMachineDetail::getE, bo.getE());
        lqw.eq(StringUtils.isNotBlank(bo.getDlm()), NLaboratoryMechanicsAllPowerfulMachineDetail::getDlm, bo.getDlm());
        lqw.eq(StringUtils.isNotBlank(bo.getAgt()), NLaboratoryMechanicsAllPowerfulMachineDetail::getAgt, bo.getAgt());
        lqw.eq(StringUtils.isNotBlank(bo.getAt()), NLaboratoryMechanicsAllPowerfulMachineDetail::getAt, bo.getAt());
        lqw.eq(StringUtils.isNotBlank(bo.getLm()), NLaboratoryMechanicsAllPowerfulMachineDetail::getLm, bo.getLm());
        lqw.eq(StringUtils.isNotBlank(bo.getAg()), NLaboratoryMechanicsAllPowerfulMachineDetail::getAg, bo.getAg());
        lqw.eq(StringUtils.isNotBlank(bo.getDltz()), NLaboratoryMechanicsAllPowerfulMachineDetail::getDltz, bo.getDltz());
        lqw.eq(StringUtils.isNotBlank(bo.getDlxz()), NLaboratoryMechanicsAllPowerfulMachineDetail::getDlxz, bo.getDlxz());
        lqw.eq(StringUtils.isNotBlank(bo.getDlwz()), NLaboratoryMechanicsAllPowerfulMachineDetail::getDlwz, bo.getDlwz());
        lqw.eq(StringUtils.isNotBlank(bo.getDatas()), NLaboratoryMechanicsAllPowerfulMachineDetail::getDatas, bo.getDatas());
        return lqw;
    }

    /**
     * 新增万能机TestMaster软件上传详情数据
     */
    @Override
    public Boolean insertByBo(NLaboratoryMechanicsAllPowerfulMachineDetailBo bo) {
        NLaboratoryMechanicsAllPowerfulMachineDetail add = BeanUtil.toBean(bo, NLaboratoryMechanicsAllPowerfulMachineDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改万能机TestMaster软件上传详情数据
     */
    @Override
    public Boolean updateByBo(NLaboratoryMechanicsAllPowerfulMachineDetailBo bo) {
        NLaboratoryMechanicsAllPowerfulMachineDetail update = BeanUtil.toBean(bo, NLaboratoryMechanicsAllPowerfulMachineDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryMechanicsAllPowerfulMachineDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除万能机TestMaster软件上传详情数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 万能机详情设备列表
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsAllPowerfulMachineDetailVo> page(NLaboratoryMechanicsAllPowerfulMachineDetailBo bo, PageQuery pageQuery) {
        if (StringUtils.isEmpty(bo.getId())) {
            throw new ServiceException("主表id不能为空");
        }
//        LambdaQueryWrapper<NLaboratoryMechanicsAllPowerfulMachineDetail> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryMechanicsAllPowerfulMachineDetailVo> result = baseMapper.page(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }
}
